package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"
)

// @Summary 新首頁
// @Description 新首頁
// @Accept  json
// @Produce  json
// @Tags Home
// @Success 200 {object} api_models.NewHomeDataResp "result"
// @Router /api/home/getNewHomeData [POST]
func (base *Controller) GetNewHomeData(c echo.Context) error {
	resp, err := base.Service.GetNewHomeData()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 首頁 app
// @Description 首頁 app
// @Accept  json
// @Produce  json
// @Tags Home
// @Success 200 {object} api_models.GetMHomeDataReq "result"
// @Router /api/home/m/getHomeData [POST]
func (base *Controller) MGetHomeData(c echo.Context) error {

	r := &api_models.GetMHomeDataReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	r.Page = string2.GetDefaultNum(r.Page, 1)
	r.Size = string2.GetDefaultNum(r.Size, 10)

	resp, err := base.Service.MGetHomeData(r)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 首页主播推荐
// @Description 首页主播推荐
// @Accept  json
// @Produce  json
// @Tags Home
// @Param   sendBody body api_models.RecommendAnchorReq true "body"
// @Success 200 {object} protobuf.RecommendAnchorResp "result"
// @Router /api/home/getHomeUserRecLive [POST]
func (base *Controller) GetHomeUserRecLive(c echo.Context) error {
	req := new(pb.RecommendAnchorReq)
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	req.Size = uint32(string2.GetDefaultNum(int(req.Size), 6))
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcLiveServer.RecommendAnchor(ctx, &pb.RecommendAnchorReq{
		Size:       req.Size,
		Uid:        uint32(c.Get("uid").(int)),
		Categoryid: req.Categoryid,
		ClientIp:   c.RealIP(),
		Platform:   int32(base.GetPlatform(c)),
	})
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp.GetRecommendAnchor(), "")
}
